home *** CD-ROM | disk | FTP | other *** search
/ PC World Interactive 9 / PC World Interactive 9 - Temmuz 1998.iso / share / internet / hot / cgi.z / guestbk.pl < prev    next >
Perl Script  |  1996-04-17  |  10KB  |  368 lines

  1. #!/usr/bin/perl
  2.  
  3. # Guestbook for the World Wide Web
  4. # Created by Matt Wright           Version 2.3.1
  5. # Created on: 4/21/95             Last Modified: 10/29/95
  6. # Consult the file README for more information and Installation Instructions.
  7.  
  8. #############################################################################
  9. # Set Variables
  10.  
  11. $guestbookurl = "http://your.host.com/~yourname/guestbk.htm";
  12. $guestbookreal = "/home/yourname/public_html/guestbk.htm";
  13. $guestlog = "/home/yourname/public_html/guestlog.htm";
  14. $cgiurl = "http://your.host/cgi-bin/guestbk.pl";
  15. # Please Note:
  16. # You will have to specify the correct path for the date command on your
  17. # system. You can type 'which date' on most systems to find the path.
  18. $date_command = "/usr/bin/date";
  19.  
  20. # Set Your Options:
  21. $mail = 0;              # 1 = Yes; 0 = No
  22. $uselog = 1;            # 1 = Yes; 0 = No
  23. $linkmail = 1;          # 1 = Yes; 0 = No
  24. $separator = 1;         # 1 = <hr>; 0 = <p>
  25. $redirection = 0;       # 1 = Yes; 0 = No
  26. $entry_order = 1;       # 1 = Newest entries added first;
  27.                         # 0 = Newest Entries added last.
  28. $remote_mail = 0;       # 1 = Yes; 0 = No
  29. $allow_html = 1;        # 1 = Yes; 0 = No
  30. $line_breaks = 0;    # 1 = Yes; 0 = No
  31.  
  32. # If you answered 1 to $mail or $remote_mail you will need to fill out 
  33. # these variables below:
  34. $mailprog = '/usr/lib/sendmail';
  35. $recipient = 'you@your.com';
  36.  
  37. # Done
  38. #############################################################################
  39.  
  40.  
  41. # Get the Date for Entry
  42. $date = `$date_command +"%A, %B %d, %Y at %T (%Z)"`; chop($date);
  43. $shortdate = `$date_command +"%D %T %Z"`; chop($shortdate);
  44.  
  45. # Get the input
  46. read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
  47.  
  48. # Split the name-value pairs
  49. @pairs = split(/&/, $buffer);
  50.  
  51. foreach $pair (@pairs) {
  52.    ($name, $value) = split(/=/, $pair);
  53.  
  54.    # Un-Webify plus signs and %-encoding
  55.    $value =~ tr/+/ /;
  56.    $value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;
  57.    $value =~ s/<!--(.|\n)*-->//g;
  58.  
  59.    if ($allow_html != 1) {
  60.       $value =~ s/<([^>]|\n)*>//g;
  61.    }
  62.  
  63.    $FORM{$name} = $value;
  64. }
  65.  
  66. # Print the Blank Response Subroutines
  67. &no_comments unless $FORM{'comments'};
  68. &no_name unless $FORM{'realname'};
  69.  
  70. # Begin the Editing of the Guestbook File
  71. open (FILE,"$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
  72. @LINES=<FILE>;
  73. close(FILE);
  74. $SIZE=@LINES;
  75.  
  76. # Open Link File to Output
  77. open (GUEST,">$guestbookreal") || die "Can't Open $guestbookreal: $!\n";
  78.  
  79. for ($i=0;$i<=$SIZE;$i++) {
  80.    $_=$LINES[$i];
  81.    if (/<!--begin-->/) { 
  82.  
  83.       if ($entry_order eq '1') {
  84.          print GUEST "<!--begin-->\n";
  85.       }
  86.    
  87.       if ($line_breaks == 1) {
  88.          $FORM{'comments'} =~ s/\cM\n/<br>\n/g;
  89.       }
  90.  
  91.       print GUEST "<b>$FORM{'comments'}</b><br>\n";
  92.  
  93.       if ($FORM{'url'}) {
  94.          print GUEST "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
  95.       }
  96.       else {
  97.          print GUEST "$FORM{'realname'}";
  98.       }
  99.  
  100.       if ( $FORM{'username'} ){
  101.          if ($linkmail eq '1') {
  102.             print GUEST " \<<a href=\"mailto:$FORM{'username'}\">";
  103.             print GUEST "$FORM{'username'}</a>\>";
  104.          }
  105.          else {
  106.             print GUEST " <$FORM{'username'}>";
  107.          }
  108.       }
  109.  
  110.       print GUEST "<br>\n";
  111.  
  112.       if ( $FORM{'city'} ){
  113.          print GUEST "$FORM{'city'},";
  114.       }
  115.      
  116.       if ( $FORM{'state'} ){
  117.          print GUEST " $FORM{'state'}";
  118.       }
  119.  
  120.       if ( $FORM{'country'} ){
  121.          print GUEST " $FORM{'country'}";
  122.       }
  123.  
  124.       if ($separator eq '1') {
  125.          print GUEST " - $date<hr>\n\n";
  126.       }
  127.       else {
  128.          print GUEST " - $date<p>\n\n";
  129.       }
  130.  
  131.       if ($entry_order eq '0') {
  132.          print GUEST "<!--begin-->\n";
  133.       }
  134.  
  135.    }
  136.    else {
  137.       print GUEST $_;
  138.    }
  139. }
  140.  
  141. close (GUEST);
  142.  
  143. # Log The Entry
  144.  
  145. if ($uselog eq '1') {
  146.    &log('entry');
  147. }
  148.  
  149.  
  150. #########
  151. # Options
  152.  
  153. # Mail Option
  154. if ($mail eq '1') {
  155.    open (MAIL, "|$mailprog $recipient") || die "Can't open $mailprog!\n";
  156.  
  157.    print MAIL "Reply-to: $FORM{'username'} ($FORM{'realname'})\n";
  158.    print MAIL "From: $FORM{'username'} ($FORM{'realname'})\n";
  159.    print MAIL "Subject: Entry to Guestbook\n\n";
  160.    print MAIL "You have a new entry in your guestbook:\n\n";
  161.    print MAIL "------------------------------------------------------\n";
  162.    print MAIL "$FORM{'comments'}\n";
  163.    print MAIL "$FORM{'realname'}";
  164.  
  165.    if ( $FORM{'username'} ){
  166.       print MAIL " <$FORM{'username'}>";
  167.    }
  168.  
  169.    print MAIL "\n";
  170.  
  171.    if ( $FORM{'city'} ){
  172.       print MAIL "$FORM{'city'},";
  173.    }
  174.  
  175.    if ( $FORM{'state'} ){
  176.       print MAIL " $FORM{'state'}";
  177.    }
  178.  
  179.    if ( $FORM{'country'} ){
  180.       print MAIL " $FORM{'country'}";
  181.    }
  182.  
  183.    print MAIL " - $date\n";
  184.    print MAIL "------------------------------------------------------\n";
  185.  
  186.    close (MAIL);
  187. }
  188.  
  189. if ($remote_mail eq '1' && $FORM{'username'}) {
  190.    open (MAIL, "|$mailprog -t") || die "Can't open $mailprog!\n";
  191.  
  192.    print MAIL "To: $FORM{'username'}\n";
  193.    print MAIL "From: $recipient\n";
  194.    print MAIL "Subject: Entry to Guestbook\n\n";
  195.    print MAIL "Thank you for adding to my guestbook.\n\n";
  196.    print MAIL "------------------------------------------------------\n";
  197.    print MAIL "$FORM{'comments'}\n";
  198.    print MAIL "$FORM{'realname'}";
  199.  
  200.    if ( $FORM{'username'} ){
  201.       print MAIL " <$FORM{'username'}>";
  202.    }
  203.  
  204.    print MAIL "\n";
  205.  
  206.    if ( $FORM{'city'} ){
  207.       print MAIL "$FORM{'city'},";
  208.    }
  209.  
  210.    if ( $FORM{'state'} ){
  211.       print MAIL " $FORM{'state'}";
  212.    }
  213.  
  214.    if ( $FORM{'country'} ){
  215.      print MAIL " $FORM{'country'}";
  216.    }
  217.  
  218.    print MAIL " - $date\n";
  219.    print MAIL "------------------------------------------------------\n";
  220.  
  221.    close (MAIL);
  222. }
  223.  
  224. # Print Out Initial Output Location Heading
  225. if ($redirection eq '1') {
  226.    print "Location: $guestbookurl\n\n";
  227. }
  228. else { 
  229.    &no_redirection;
  230. }
  231.  
  232. #######################
  233. # Subroutines
  234.  
  235. sub no_comments {
  236.    print "Content-type: text/html\n\n";
  237.    print "<html><head><title>No Comments</title></head>\n";
  238.    print "<body><h1>Your Comments appear to be blank</h1>\n";
  239.    print "The comment section in the guestbook fillout form appears\n";
  240.    print "to be blank and therefore the Guestbook Addition was not\n";
  241.    print "added.  Please enter your comments below.<p>\n";
  242.    print "<form method=POST action=\"$cgiurl\">\n";
  243.    print "Your Name:<input type=text name=\"realname\" size=30 ";
  244.    print "value=\"$FORM{'realname'}\"><br>\n";
  245.    print "E-Mail: <input type=text name=\"username\""; 
  246.    print "value=\"$FORM{'username'}\" size=40><br>\n";
  247.    print "City: <input type=text name=\"city\" value=\"$FORM{'city'}\" ";
  248.    print "size=15>, State: <input type=text name=\"state\" "; 
  249.    print "value=\"$FORM{'state'}\" size=15> Country: <input type=text "; 
  250.    print "name=\"country\" value=\"$FORM{'country'}\" size=15><p>\n";
  251.    print "Comments:<br>\n";
  252.    print "<textarea name=\"comments\" COLS=60 ROWS=4></textarea><p>\n";
  253.    print "<input type=submit> * <input type=reset></form><hr>\n";
  254.    print "Return to the <a href=\"$guestbookurl\">Guestbook</a>.";
  255.    print "\n</body></html>\n";
  256.  
  257.    # Log The Error
  258.    if ($uselog eq '1') {
  259.       &log('no_comments');
  260.    }
  261.  
  262.    exit;
  263. }
  264.  
  265. sub no_name {
  266.    print "Content-type: text/html\n\n";
  267.    print "<html><head><title>No Name</title></head>\n";
  268.    print "<body><h1>Your Name appears to be blank</h1>\n";
  269.    print "The Name Section in the guestbook fillout form appears to\n";
  270.    print "be blank and therefore your entry to the guestbook was not\n";
  271.    print "added.  Please add your name in the blank below.<p>\n";
  272.    print "<form method=POST action=\"$cgiurl\">\n";
  273.    print "Your Name:<input type=text name=\"realname\" size=30><br>\n";
  274.    print "E-Mail: <input type=text name=\"username\"";
  275.    print " value=\"$FORM{'username'}\" size=40><br>\n";
  276.    print "City: <input type=text name=\"city\" value=\"$FORM{'city'}\" ";
  277.    print "size=15>, State: <input type=text name=\"state\" ";
  278.    print "value=\"$FORM{'state'}\" size=2> Country: <input type=text ";
  279.    print "value=USA name=\"country\" value=\"$FORM{'country'}\" ";
  280.    print "size=15><p>\n";
  281.    print "Comments have been retained.<p>\n";
  282.    print "<input type=hidden name=\"comments\" ";
  283.    print "value=\"$FORM{'comments'}\">\n";
  284.    print "<input type=submit> * <input type=reset><hr>\n";
  285.    print "Return to the <a href=\"$guestbookurl\">Guestbook</a>.";
  286.    print "\n</body></html>\n";
  287.  
  288.    # Log The Error
  289.    if ($uselog eq '1') {
  290.       &log('no_name');
  291.    }
  292.  
  293.    exit;
  294. }
  295.  
  296. # Log the Entry or Error
  297. sub log {
  298.    $log_type = $_[0];
  299.    open (LOG, ">>$guestlog");
  300.    if ($log_type eq 'entry') {
  301.       print LOG "$ENV{'REMOTE_HOST'} - [$shortdate]<br>\n";
  302.    }
  303.    elsif ($log_type eq 'no_name') {
  304.       print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No Name<br>\n";
  305.    }
  306.    elsif ($log_type eq 'no_comments') {
  307.       print LOG "$ENV{'REMOTE_HOST'} - [$shortdate] - ERR: No ";
  308.       print LOG "Comments<br>\n";
  309.    }
  310. }
  311.  
  312. # Redirection Option
  313. sub no_redirection {
  314.  
  315.    # Print Beginning of HTML
  316.    print "Content-Type: text/html\n\n";
  317.    print "<html><head><title>Thank You</title></head>\n";
  318.    print "<body><h1>Thank You For Signing The Guestbook</h1>\n";
  319.  
  320.    # Print Response
  321.    print "Thank you for filling in the guestbook.  Your entry has\n";
  322.    print "been added to the guestbook.<hr>\n";
  323.    print "Here is what you submitted:<p>\n";
  324.    print "<b>$FORM{'comments'}</b><br>\n";
  325.  
  326.    if ($FORM{'url'}) {
  327.       print "<a href=\"$FORM{'url'}\">$FORM{'realname'}</a>";
  328.    }
  329.    else {
  330.       print "$FORM{'realname'}";
  331.    }
  332.  
  333.    if ( $FORM{'username'} ){
  334.       if ($linkmail eq '1') {
  335.          print " <<a href=\"mailto:$FORM{'username'}\">";
  336.          print "$FORM{'username'}</a>>";
  337.       }
  338.       else {
  339.          print " <$FORM{'username'}>";
  340.       }
  341.    }
  342.  
  343.    print "<br>\n";
  344.  
  345.    if ( $FORM{'city'} ){
  346.       print "$FORM{'city'},";
  347.    }
  348.  
  349.    if ( $FORM{'state'} ){
  350.       print " $FORM{'state'}";
  351.    }
  352.  
  353.    if ( $FORM{'country'} ){
  354.       print " $FORM{'country'}";
  355.    }
  356.  
  357.    print " - $date<p>\n";
  358.  
  359.    # Print End of HTML
  360.    print "<hr>\n";
  361.    print "<a href=\"$guestbookurl\">Back to the Guestbook</a>\n";         print "- You may need to reload it when you get there to see your\n";
  362.    print "entry.\n";
  363.    print "</body></html>\n";
  364.  
  365.    exit;
  366. }
  367.  
  368.